//
//  DateSelecter.m
//  Traveling
//
//  Created by 康苗 on 15/6/13.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//

#import "DateSelecter.h"

#define DatePickerHeight 216

@interface DateSelecter()
@property (weak, nonatomic) UIButton *btn;
@property (weak, nonatomic) UIDatePicker *picker;

@end

@implementation DateSelecter

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setSize:CGSizeMake(SCREENW, SCREENH)];
//        self.backgroundColor = [UIColor blackColor];
        UIButton *btn = [[UIButton alloc]initWithFrame:self.frame];
        [btn setBackgroundColor:[UIColor clearColor]];
        [btn addTarget:self action:@selector(hidden:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        self.btn = btn;
        
        UIDatePicker *pick = [[UIDatePicker alloc] init];
        pick.datePickerMode = UIDatePickerModeDate;
        [pick addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
        [self addSubview:pick];
        self.picker = pick;
    }
    return self;
}

- (void)showWithDate:(NSDate *)date{
    if (date) {
        self.picker.date = date;
    }
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.picker.y = self.height;
    [UIView animateWithDuration:0.25 animations:^{
        self.picker.y = self.height - DatePickerHeight;
    }];

}

- (void)layoutSubviews{
    self.picker.x = 0;
    self.picker.width = self.width;
    self.picker.height = DatePickerHeight;
}

- (void)hidden:(UIButton *)sender{
    [UIView animateWithDuration:0.25 animations:^{
        self.picker.y = self.height;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

-(void)dateChanged:(UIDatePicker *)sender{
    if ([self.delegate respondsToSelector:@selector(selectedWithDate:)]) {
        [self.delegate selectedWithDate:sender.date];
    }

}

@end
